//
//  AddPersonalLabelViewController.m
//  SouthChina
//
//  Created by kc on 15/7/8.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "AddPersonalLabelViewController.h"
#import "QRadioButton.h"
@interface AddPersonalLabelViewController ()
{
    NSMutableArray *personallabel;         //个人标签
    NSString *checklabel;//选择标签
}
@property (strong, nonatomic) IBOutlet UITextField *userlabel;
@property (strong, nonatomic) IBOutlet UIButton *btAddLabel;
@end

@implementation AddPersonalLabelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    personallabel = [[NSMutableArray alloc]init];
    [self addTitleBarWithBackBtn:@"添加标签" needReturn:YES];
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"AddPersonalLabelViewBody" owner:self options:nil] objectAtIndex:0];
    viewBody.frame = (CGRect){0, self.titlebar.frame.origin.y+self.titlebar.frame.size.height,SCREEN_WIDTH, SCREEN_HEIGHT-self.titlebar.frame.size.height};
    [self.view addSubview:viewBody];
    
    //单选框
    QRadioButton *radio1 = [[QRadioButton alloc]initWithDelegate:self groupId:@"remaind"];
    radio1.frame = CGRectMake(_userlabel.frame.origin.x,_userlabel.frame.origin.y+130 ,100, 50);
    [radio1 setTitle:@"看电影" forState:UIControlStateNormal];
    [radio1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [radio1.titleLabel setFont:[UIFont fontWithName:@"" size:14.0f]];
    [self.view addSubview:radio1];
    
    [radio1 setChecked:YES];
    QRadioButton *radio2 = [[QRadioButton alloc]initWithDelegate:self groupId:@"remaind"];
    radio2.frame = CGRectMake(_userlabel.frame.origin.x,_userlabel.frame.origin.y+192 ,100, 50);
    [radio2 setTitle:@"非主流" forState:UIControlStateNormal];
    [radio2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [radio2.titleLabel setFont:[UIFont fontWithName:@"" size:14.0f]];
    [self.view addSubview:radio2];
    
    QRadioButton *radio3 = [[QRadioButton alloc]initWithDelegate:self groupId:@"remaind"];
    radio3.frame = CGRectMake(_userlabel.frame.origin.x,_userlabel.frame.origin.y+254 ,100, 50);
    [radio3 setTitle:@"长沙星城" forState:UIControlStateNormal];
    [radio3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [radio3.titleLabel setFont:[UIFont fontWithName:@"" size:14.0f]];
    [self.view addSubview:radio3];
    
    checklabel=@"看电影"; //初始化
    _userlabel.placeholder = @"自定义标签";
    _userlabel.clearsOnBeginEditing = YES;    //再次编辑就清空
    _userlabel.keyboardType = UIKeyboardTypeNumberPad;   //设置键盘的样式
    
    //添加按钮增加边框
    [_btAddLabel.layer setMasksToBounds:YES];//设置按钮的圆角半径不会被遮挡
    [_btAddLabel.layer setBorderWidth:1];//设置边界的宽度
    //设置按钮的边界颜色
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    
   
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){0,0.1,0.1,1});
    
    [_btAddLabel.layer setBorderColor:color];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//相应方法：
-(void)didSelectedRadioButton:(QRadioButton *)radio groupId:(NSString *)groupId
{
    if ([radio.titleLabel.text isEqualToString:@"看电影"]) {
        checklabel=@"看电影";
    }
    if ([radio.titleLabel.text isEqualToString:@"非主流"]) {
        checklabel=@"非主流";
    }
    if ([radio.titleLabel.text isEqualToString:@"长沙星城"]) {
        checklabel=@"长沙星城";
    }
    
    // [self.checkBox setImage:[UIImage imageNamed:isSelected?@"radio_checked":@"radio_unchecked"]];
}

//提交新加标签
- (IBAction)btClickAddLabel:(id)sender {
    
    //设置个人标签最多10个
    NSString *sinittags = [DE getGlobalVar:LOGIN_USER_TAGS] == nil?@"":[DE getGlobalVar:LOGIN_USER_TAGS];
    NSLog(@"读取初始标签：%@",sinittags);
    NSArray *list=[sinittags componentsSeparatedByString:@"，"];
    NSLog(@"标签初始化大小：%lu",[list count]);
    if ([list count]>=10) {
        [self showAlerHUD:@"你的个人标签已经达到了最多为6个的限制"];
        return;
    }
    //先清空所有对象
    [personallabel removeAllObjects];
    //增加标签
    if(_userlabel.text.length == 0 )
    {
        NSLog(@"增加为空");
    }
    else{
         if([_userlabel.text isEqualToString:@"自定义标签"])
              NSLog(@"增加自定义标签");
         else
         {
             NSLog(@"增加标签%@",_userlabel.text);
             [personallabel addObject: _userlabel.text];//增加子定义标签
         }
        
    }
    
    //增加单选标签
    [personallabel addObject: checklabel];
    
    //标签格式化入库
    NSString *stagslist=@"";
    for(NSInteger i = 0;i<personallabel.count;i++)
    {
        NSLog(@"用户选择和自定义标签为%@",[personallabel objectAtIndex:i] );
        if(i != (personallabel.count-1)) //追加,隔开
        {
           stagslist = [stagslist stringByAppendingString:[[personallabel objectAtIndex:i] stringByAppendingString:@"，"]];
        }
        else
        {
            stagslist = [stagslist stringByAppendingString:[personallabel objectAtIndex:i]];
        }
    }
    NSLog(@"最终写入数据库的标签为：%@",stagslist);
    
    if(![sinittags isEqualToString:@""])
    {
        sinittags = [@"，" stringByAppendingString:sinittags];
        stagslist = [stagslist stringByAppendingString:sinittags];
    }
    NSLog(@"最终写入数据库的标签为00：%@",stagslist);
    NSDictionary *map = @{@"userTags":stagslist};
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DE serverCall:@"updateUserInfo" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            [DE setGlobalVar:LOGIN_USER_TAGS value:stagslist];
            [self showAlerHUD:@"提交成功！"];
        }
        else{
            [self showAlerHUD:@"提交失败！"];
        }
    }];
    
}
@end
